<script setup lang="ts">
import { computed, ref } from 'vue'
import { useCartStore } from '@/stores/modules/cart'
import { onShow } from '@dcloudio/uni-app'
import type { OrderItem } from '@/types/order'
import { submitOrderApi } from '@/services/order.ts'
import type { Product } from '@/types'
const cartStore = useCartStore()
// 选中的商品
const selectedProducts = cartStore.getSelectedProductsList()
console.log(selectedProducts)
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 订单备注
const buyerMessage = ref('')
// 收货人
const buyerName = ref('123')
// 收货人电话
const buyerPhone = ref('18671435915')
// // 配送时间
// const deliveryList = ref([
//   { type: 1, text: '时间不限 (周一至周日)' },
//   { type: 2, text: '工作日送 (周一至周五)' },
//   { type: 3, text: '周末配送 (周六至周日)' },
// ])
// 当前配送时间下标
// const activeIndex = ref(0)
// // 当前配送时间
// const activeDelivery = computed(() => deliveryList.value[activeIndex.value])
// // 修改配送时间
// const onChangeDelivery: UniHelper.SelectorPickerOnChange = (ev) => {
//   activeIndex.value = ev.detail.value
// }

// 提交订单
const onSubmit = async () => {
  // 校验收货人电话
  if (!buyerPhone.value && !buyerPhone.value.match(/^1[3456789]\d{9}$/)) {
    return uni.showToast({
      title: '请输入正确的电话号码',
      icon: 'none',
      duration: 1000,
    })
  }
  if (!buyerName.value.trim()) {
    return uni.showToast({
      title: '请输入收货人',
      icon: 'none',
      duration: 1000,
    })
  }
  // 模态框确认
  uni.showModal({
    title: '确认提交订单吗？',
    content: '请确认收货地址及商品信息',
    success: async (res) => {
      if (res.confirm) {
        const resp = await submitOrderApi({
          Goodslist: selectedProducts.map((item: Product) => {
            return {
              goodsId: item.id,
              number: item.count,
            }
          }),
          phone: buyerPhone.value,
          amount: cartStore.getSelectedProductPrice(),
          consignee: buyerName.value,
          remark: buyerMessage.value,
        })
        if (resp.code === 0) {
          cartStore.deleteSelectedProducts()
          setTimeout(() => {
            uni.showToast({
              title: '提交成功',
              icon: 'none',
            })
          }, 500)
          uni.switchTab({
            url: '/pages/my/my',
          })
        } else {
          uni.showToast({
            title: resp.msg,
            icon: 'none',
            duration: 1000,
          })
        }
      }
    },
  })
}
onShow(() => {})
</script>

<template>
  <scroll-view scroll-y class="viewport">
    <!-- 收货地址 -->
    <!--    <navigator-->
    <!--      v-if="false"-->
    <!--      class="shipment"-->
    <!--      hover-class="none"-->
    <!--      url="/pagesMember/address/address?from=order"-->
    <!--    >-->
    <!--      <view class="user"> 张三 13333333333 </view>-->
    <!--      <view class="address"> 广东省 广州市 天河区 黑马程序员3 </view>-->
    <!--      <text class="icon icon-right"></text>-->
    <!--    </navigator>-->
    <!--    <navigator-->
    <!--      v-else-->
    <!--      class="shipment"-->
    <!--      hover-class="none"-->
    <!--      url="/pagesMember/address/address?from=order"-->
    <!--    >-->
    <!--      <view class="address"> 请选择收货地址 </view>-->
    <!--      <text class="icon icon-right"></text>-->
    <!--    </navigator>-->

    <!-- 商品信息 -->
    <view class="goods">
      <view v-for="item in selectedProducts" :key="item.id" class="item" hover-class="none">
        <image class="picture" :src="item.image" />
        <view class="meta">
          <view class="name ellipsis"> {{ item.name }} </view>
          <!--          <view class="attrs">藏青小花 130</view>-->
          <view class="prices">
            <view class="pay-price symbol">{{ item.price }}</view>
            <!--            <view class="price symbol">99.00</view>-->
          </view>
          <view class="count">x{{ item.count }}</view>
        </view>
      </view>
    </view>

    <!-- 配送及支付方式 -->
    <view class="related">
      <!--      <view class="item">-->
      <!--        <text class="text">提货时间</text>-->
      <!--        <picker :range="deliveryList" range-key="text" @change="onChangeDelivery">-->
      <!--          <view class="icon-fonts picker">{{ activeDelivery.text }}</view>-->
      <!--        </picker>-->
      <!--      </view>-->
      <view class="item">
        <text class="text">订单备注</text>
        <input
          class="input"
          :cursor-spacing="30"
          placeholder="所有订单统一默认在周天家中取"
          v-model="buyerMessage"
        />
      </view>
      <view class="item">
        <text class="text">收货人</text>
        <input class="input" :cursor-spacing="30" v-model="buyerName" />
      </view>
      <view class="item">
        <text class="text">电话号码</text>
        <input class="input" :cursor-spacing="30" v-model="buyerPhone" />
      </view>
    </view>

    <!-- 支付金额 -->
    <view class="settlement">
      <view class="item">
        <text class="text">商品总价: </text>
        <text class="number symbol">{{ cartStore.getSelectedProductPrice() }}</text>
      </view>
      <view class="item">
        <text class="text">运费: </text>
        <text class="number symbol">待定</text>
      </view>
    </view>
  </scroll-view>

  <!-- 吸底工具栏 -->
  <view class="toolbar" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }">
    <view class="total-pay symbol">
      <text class="number"> 价格加运费 </text>
    </view>
    <view class="button" @click="onSubmit"> 提交订单 </view>
  </view>
</template>

<style lang="scss">
page {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.symbol::before {
  content: '¥';
  margin-right: 5rpx;
}

.shipment {
  margin: 20rpx;
  padding: 30rpx 30rpx 30rpx 84rpx;
  font-size: 26rpx;
  border-radius: 10rpx;
  background: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png)
    20rpx center / 50rpx no-repeat #fff;
  position: relative;

  .icon {
    font-size: 36rpx;
    color: #333;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 20rpx;
  }

  .user {
    color: #333;
    margin-bottom: 5rpx;
  }

  .address {
    color: #666;
  }
}

.goods {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    padding: 30rpx 0;
    border-top: 1rpx solid #eee;

    &:first-child {
      border-top: none;
    }

    .picture {
      width: 170rpx;
      height: 170rpx;
      border-radius: 10rpx;
      margin-right: 20rpx;
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: relative;
    }

    .name {
      height: 80rpx;
      font-size: 26rpx;
      color: #444;
    }

    .attrs {
      line-height: 1.8;
      padding: 0 15rpx;
      margin-top: 6rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .prices {
      display: flex;
      align-items: baseline;
      margin-top: 6rpx;
      font-size: 28rpx;

      .pay-price {
        font-size: 16px;
        margin-right: 10rpx;
        color: #cf4444;
      }

      .price {
        font-size: 24rpx;
        color: #999;
        text-decoration: line-through;
      }
    }

    .count {
      position: absolute;
      bottom: 0;
      right: 0;
      font-size: 26rpx;
      color: #444;
    }
  }
}

.related {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 80rpx;
    font-size: 26rpx;
    color: #333;
  }

  .input {
    flex: 1;
    text-align: right;
    margin: 20rpx 0;
    padding-right: 20rpx;
    font-size: 26rpx;
    color: black;
  }

  .item .text {
    width: 125rpx;
  }

  .picker {
    color: #666;
  }

  .picker::after {
    content: '\e6c2';
  }
}

/* 结算清单 */
.settlement {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80rpx;
    font-size: 26rpx;
    color: #333;
  }

  .danger {
    color: #cf4444;
  }
}

/* 吸底工具栏 */
.toolbar {
  position: sticky;
  left: 0;
  right: 0;
  bottom: var(--window-bottom);
  z-index: 1;

  background-color: #fff;
  height: 100rpx;
  padding: 0 20rpx;
  border-top: 1rpx solid #eaeaea;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: content-box;

  .total-pay {
    font-size: 40rpx;
    color: #cf4444;

    .decimal {
      font-size: 75%;
    }
  }

  .button {
    width: 220rpx;
    text-align: center;
    line-height: 72rpx;
    font-size: 26rpx;
    color: #fff;
    border-radius: 72rpx;
    background-color: #27ba9b;
  }

  .disabled {
    opacity: 0.6;
  }
}
</style>
